﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyERP.Framework.Plugin;
using TinyERP.Framework.Config;
using System.Globalization;
using System.Threading;
using System.Reflection;
using TinyERP.Core.Business;
using TinyERP.Framework;
using System.Collections;
using TinyERP.Core.DTO;
using Bamboo.Core.Data;
using TinyERP.Core;

namespace TinyERPApp
{
    public class ApplicationLoader
    {
        private const string APP_CONFIG_RESOURCE = "TinyERPApp.ApplicationConfig.Application.xml";
        private static IModuleService ModuleService
        {
            get
            {
                return (IModuleService)ServiceLocator.GetService("ModuleServiceProxy");
            }
        }

        private static IModuleRegisterService ModuleRegisterService
        {
            get
            {
                return (IModuleRegisterService)ServiceLocator.GetService("ModuleRegisterServiceProxy");
            }
        }
        private static IUserService UserService
        {
            get
            {
                return (IUserService)ServiceLocator.GetService("UserServiceProxy");
            }
        }
        private static IUserGroupService UserGroupService
        {
            get
            {
                return (IUserGroupService)ServiceLocator.GetService("UserGroupServiceProxy");
            }
        }
        private static void InitLogging()
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        public static void Run()
        {
            InitLogging();
            LoadConfiguration();
            LoadPlugins();
            LoadLocale();
            ApplicationInfo.LoadInfo();
            SetupProgramAtFirstTime();
        }
        private static void LoadPlugins()
        {
            PluginManager.GetInstance().LoadPlugins();
            RegisterAllModulesInDB();
        }

        private static void RegisterAllModulesInDB()
        {
            IList<TinyERP.Framework.Config.ModuleElement> moduleElementList = ApplicationConfiguration.GetInstance().Modules;
            foreach (TinyERP.Framework.Config.ModuleElement moduleElement in moduleElementList)
            {
                ModuleRegisterService.RegisterModuleConfig(moduleElement);
            }
        }
        private static void LoadConfiguration()
        {
            IApplicationConfigProvider configurationProvider = new XmlConfigurationProvider(Assembly.GetEntryAssembly(),
                                                APP_CONFIG_RESOURCE);
            configurationProvider.LoadConfiguration(ApplicationConfiguration.GetInstance());

        }
        private static void SetupProgramAtFirstTime()
        {
            long totalRecord = 0;
            IList userList = UserService.GetAllUser("ID", Bamboo.Core.Data.SortDirection.ASCENDING, out totalRecord);
            bool firstTimeToStartProgram = userList.Count == 0;
            if (!firstTimeToStartProgram)
            {
                return;
            }
             UserGroupDTO adminGroup = SetupAdminGroup();
             SetupAdminUser(adminGroup);
        }
        private static void SetupAdminUser(UserGroupDTO adminGroup)
        {
            UserDTO adminUser = new UserDTO();
            adminUser.UserName = "admin";
            adminUser.RawPassword = "admin";
            adminUser.IsSystemAdmin = true;
            adminUser.UserGroupID = adminGroup.ID;
            UserService.SaveUser(adminUser);
        }
        private static UserGroupDTO SetupAdminGroup()
        {
            UserGroupDTO group = new UserGroupDTO();
            group.Name = "Administrator";
            group.Description = "System administrator";
            long groupID = (long) UserGroupService.SaveUserGroup(group);
            group.ID = groupID;
            return group;

        }
        private static void LoadLocale()
        {
            string language = ApplicationConfiguration.GetInstance().DefaultLanguage;
            CultureInfo cultureInfo = new CultureInfo(language);
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }

        
    }
}
